package com.huanke.iot.api.service.device.wb;


import com.huanke.iot.base.dao.device.DeviceTeamItemMapper;
import com.huanke.iot.base.dao.device.DeviceTeamMapper;
import com.huanke.iot.base.dao.wb.MaintenanceOrderHistoryMapper;
import com.huanke.iot.base.dao.wb.MaintenanceOrderMapper;
import com.huanke.iot.base.dao.wb.MaintenanceRecordMapper;
import com.huanke.iot.base.dao.wb.MaintenanceUserMapper;
import com.huanke.iot.base.dto.wb.MaintenanceOrderDetailVo;
import com.huanke.iot.base.dto.wb.MaintenanceOrderVo;
import com.huanke.iot.base.dto.wb.MaintenanceUserMsgVo;
import com.huanke.iot.base.dto.wb.MaintenanceWbUserVo;
import com.huanke.iot.base.po.device.team.DeviceTeamItemPo;
import com.huanke.iot.base.po.device.team.DeviceTeamPo;
import com.huanke.iot.base.po.wb.MaintenanceOrder;
import com.huanke.iot.base.po.wb.MaintenanceOrderHistory;
import com.huanke.iot.base.po.wb.MaintenanceUser;
import com.huanke.iot.base.request.wb.DistributeRequest;
import com.huanke.iot.base.request.wb.FeedbackRequest;
import com.huanke.iot.base.request.wb.MaintenanceManageLoginRequest;
import com.huanke.iot.base.request.wb.MaintenanceOrderRequest;
import com.huanke.iot.base.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Repository
@Slf4j
public class MaintenanceService {
    private static int i = 1;


    private final static String OPRATE_DISTRIBUTE = "管理员-派单";
    private final static String OPRATE_IGNORE = "管理员-忽略";
    private final static String OPRATE_REFUSE = "管理员-拒绝";
    private final static String OPRATE_FINISH = "管理员-完成";
    private final static String OPRATE_RESOLVE = "业主-已解决";
    private final static String OPRATE_UNRESOLVE = "业主-未解决";
    private final static String WB_TEAM_NAME = "维保组";

    @Autowired
    private MaintenanceOrderMapper maintenanceOrderMapper;
    @Autowired
    private MaintenanceRecordMapper maintenanceRecordMapper;
    @Autowired
    private MaintenanceUserMapper maintenanceUserMapper;
    @Autowired
    private MaintenanceOrderHistoryMapper maintenanceOrderHistoryMapper;
    @Autowired
    private DeviceTeamMapper deviceTeamMapper;
    @Autowired
    private DeviceTeamItemMapper deviceTeamItemMapper;
    @Value("${saltEncrypt}")
    private String saltEncrypt;

    public String addOrder(MaintenanceOrderRequest request, Integer customerUserId, Integer customerId) {
        try {
            MaintenanceUser user = getMaintenanceUser(customerUserId);

            request.setUserId(user.getId());
            MaintenanceOrder order  = new MaintenanceOrder();
            BeanUtils.copyProperties(request, order);
            order.setFaultPhoto(String.join(",",request.getFaultPhoto()));
            order.setCustomerId(customerId);
            order.setFeedback(0);  //业主反馈状态：0初始状态，1已解决，2未解决
            order.setPreStatus(1);  //分配前，状态：1待审核（管理员待分配），2待维修，3已忽略
            order.setWbStatus(1);  //分配后，状态：0初始状态，1待维修，2待确认（管理员待审核），3已完成，4已拒绝
            order.setOrderTime(new Date()); //订单时间
            LocalDateTime now = LocalDateTime.now();
            //年月日时分秒毫秒
            String orderNo = now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")) + afterAdd();
            order.setOrderNo(orderNo);
            order.setStatus(1);
            order.setPjStatus(0);
            maintenanceOrderMapper.insert(order);
            List<MaintenanceUser> userList = maintenanceUserMapper.selectManageUserList(customerId);
            if(userList.size()>0){
                return userList.get(0).getPhone();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



    private synchronized String afterAdd() {
        if(i<999){
            i++;
        }else{
            i=1;
        }
        return String.format("%03d", i);
    }

    public List<MaintenanceOrderVo> orderlist(Integer customerUserId, Integer orderStatus) {
        List<MaintenanceOrderVo> maintenanceOrderVoList = new ArrayList<>();
        switch (orderStatus) {
            case 0:  //全部
                List<MaintenanceOrder> maintenanceOrders = maintenanceOrderMapper.selectOrderList(customerUserId, orderStatus);
                for (MaintenanceOrder maintenanceOrder : maintenanceOrders) {
                    MaintenanceOrderVo maintenanceOrderVo = new MaintenanceOrderVo();
                    BeanUtils.copyProperties(maintenanceOrder, maintenanceOrderVo);
                    getResultStatus(maintenanceOrder, maintenanceOrderVo);
                    maintenanceOrderVoList.add(maintenanceOrderVo);
                }
                break;
            case 1:  //待维修
                List<MaintenanceOrder> maintenanceOrders1 = maintenanceOrderMapper.selectOrderList1(customerUserId, orderStatus);
                for (MaintenanceOrder maintenanceOrder : maintenanceOrders1) {
                    MaintenanceOrderVo maintenanceOrderVo = new MaintenanceOrderVo();
                    BeanUtils.copyProperties(maintenanceOrder, maintenanceOrderVo);
                    Integer preStatus = maintenanceOrder.getPreStatus();
                    maintenanceOrderVo.setResultStatus(2);
                    maintenanceOrderVo.setResultStatusMsg("待维修");
                    maintenanceOrderVoList.add(maintenanceOrderVo);
                }
                break;
            case 2:   //待确认
                List<MaintenanceOrder> maintenanceOrders2 = maintenanceOrderMapper.selectOrderList2(customerUserId, orderStatus);
                for (MaintenanceOrder maintenanceOrder : maintenanceOrders2) {
                    MaintenanceOrderVo maintenanceOrderVo = new MaintenanceOrderVo();
                    BeanUtils.copyProperties(maintenanceOrder, maintenanceOrderVo);
                    maintenanceOrderVo.setResultStatus(3);
                    maintenanceOrderVo.setResultStatusMsg("待确认");
                    maintenanceOrderVoList.add(maintenanceOrderVo);
                }
                break;
            case 3:   //已完成
                List<MaintenanceOrder> maintenanceOrders3 = maintenanceOrderMapper.selectOrderList3(customerUserId, orderStatus);
                for (MaintenanceOrder maintenanceOrder : maintenanceOrders3) {
                    MaintenanceOrderVo maintenanceOrderVo = new MaintenanceOrderVo();
                    BeanUtils.copyProperties(maintenanceOrder, maintenanceOrderVo);
                    Integer feedback = maintenanceOrder.getFeedback();
                    if(feedback == 2){
                        maintenanceOrderVo.setResultStatus(5);
                        maintenanceOrderVo.setResultStatusMsg("未解决");
                    }else{
                        maintenanceOrderVo.setResultStatus(4);
                        maintenanceOrderVo.setResultStatusMsg("已解决");
                    }
                    maintenanceOrderVoList.add(maintenanceOrderVo);
                }
                break;
            default:
                break;
        }
        return maintenanceOrderVoList;
    }

    public List<MaintenanceOrderVo> wbOrderlist(Integer customerUserId, Integer orderStatus) {
        List<MaintenanceOrderVo> maintenanceOrderVoList = new ArrayList<>();
        switch (orderStatus) {
            case 0:  //全部
                List<MaintenanceOrder> maintenanceOrders = maintenanceOrderMapper.selectWbOrderList(customerUserId, orderStatus);
                for (MaintenanceOrder maintenanceOrder : maintenanceOrders) {
                    MaintenanceOrderVo maintenanceOrderVo = new MaintenanceOrderVo();
                    BeanUtils.copyProperties(maintenanceOrder, maintenanceOrderVo);
                    getWbResultStatus(maintenanceOrder, maintenanceOrderVo);
                    maintenanceOrderVoList.add(maintenanceOrderVo);
                }
                break;
            case 1:  //待维修
                List<MaintenanceOrder> maintenanceOrders1 = maintenanceOrderMapper.selectWbOrderList1(customerUserId, orderStatus);
                for (MaintenanceOrder maintenanceOrder : maintenanceOrders1) {
                    MaintenanceOrderVo maintenanceOrderVo = new MaintenanceOrderVo();
                    BeanUtils.copyProperties(maintenanceOrder, maintenanceOrderVo);
                    getWbResultStatus(maintenanceOrder, maintenanceOrderVo);
                    maintenanceOrderVoList.add(maintenanceOrderVo);
                }
                break;
            case 2:  //待确认
                List<MaintenanceOrder> maintenanceOrders2 = maintenanceOrderMapper.selectWbOrderList2(customerUserId, orderStatus);
                for (MaintenanceOrder maintenanceOrder : maintenanceOrders2) {
                    MaintenanceOrderVo maintenanceOrderVo = new MaintenanceOrderVo();
                    BeanUtils.copyProperties(maintenanceOrder, maintenanceOrderVo);
                    getWbResultStatus(maintenanceOrder, maintenanceOrderVo);
                    maintenanceOrderVoList.add(maintenanceOrderVo);
                }
                break;
            case 3:  //已驳回
                List<MaintenanceOrder> maintenanceOrders3 = maintenanceOrderMapper.selectWbOrderList3(customerUserId, orderStatus);
                for (MaintenanceOrder maintenanceOrder : maintenanceOrders3) {
                    MaintenanceOrderVo maintenanceOrderVo = new MaintenanceOrderVo();
                    BeanUtils.copyProperties(maintenanceOrder, maintenanceOrderVo);
                    getWbResultStatus(maintenanceOrder, maintenanceOrderVo);
                    maintenanceOrderVoList.add(maintenanceOrderVo);
                }
                break;
            case 4:  //已完成
                List<MaintenanceOrder> maintenanceOrders4 = maintenanceOrderMapper.selectWbOrderList4(customerUserId, orderStatus);
                for (MaintenanceOrder maintenanceOrder : maintenanceOrders4) {
                    MaintenanceOrderVo maintenanceOrderVo = new MaintenanceOrderVo();
                    BeanUtils.copyProperties(maintenanceOrder, maintenanceOrderVo);
                    getWbResultStatus(maintenanceOrder, maintenanceOrderVo);
                    maintenanceOrderVoList.add(maintenanceOrderVo);
                }
                break;
            default:
                break;
        }
        return maintenanceOrderVoList;
    }

    private MaintenanceOrderVo getResultStatus(MaintenanceOrder maintenanceOrder, MaintenanceOrderVo maintenanceOrderVo) {
        Integer preStatus = maintenanceOrder.getPreStatus();
        Integer wbStatus = maintenanceOrder.getWbStatus();
        switch (preStatus) {
            case 1:     //1待审核（管理员待分配），2待维修，3已忽略
                maintenanceOrderVo.setResultStatus(1);
                maintenanceOrderVo.setResultStatusMsg("正在派单");
                break;
            case 2:
                switch (wbStatus) {
                    case 1:     //1待维修，2待确认（管理员待审核），3已完成，4已拒绝
                        maintenanceOrderVo.setResultStatus(2);
                        maintenanceOrderVo.setResultStatusMsg("维修人员已接单");
                        break;
                    case 2:
                        if(maintenanceOrder.getFeedback() !=0){
                            maintenanceOrderVo.setResultStatus(8);
                            maintenanceOrderVo.setResultStatusMsg("待审核");
                        }else{
                            maintenanceOrderVo.setResultStatus(3);
                            maintenanceOrderVo.setResultStatusMsg("待确认");
                        }
                        break;
                    case 3:
                        Integer feedback = maintenanceOrder.getFeedback();
                        if(feedback == 2){
                            maintenanceOrderVo.setResultStatus(5);
                            maintenanceOrderVo.setResultStatusMsg("未解决");
                        }else{
                            maintenanceOrderVo.setResultStatus(4);
                            maintenanceOrderVo.setResultStatusMsg("已解决");
                        }
                        break;
                    case 4:
                        maintenanceOrderVo.setResultStatus(6);
                        maintenanceOrderVo.setResultStatusMsg("已拒绝");
                        break;
                    default:
                        break;
                }
                break;
            case 3:
                maintenanceOrderVo.setResultStatus(7);
                maintenanceOrderVo.setResultStatusMsg("已忽略");
                break;
            default:
                break;
        }
        return maintenanceOrderVo;
    }

    private MaintenanceOrderVo getWbResultStatus(MaintenanceOrder maintenanceOrder, MaintenanceOrderVo maintenanceOrderVo) {
        Integer wbStatus = maintenanceOrder.getWbStatus();
        switch (wbStatus) {
            case 1:     //1待维修，2待确认（管理员待审核），3已完成，4已拒绝
                maintenanceOrderVo.setResultStatus(1);
                maintenanceOrderVo.setResultStatusMsg("待维修");
                break;
            case 2:
                maintenanceOrderVo.setResultStatus(2);
                maintenanceOrderVo.setResultStatusMsg("已维修");
                break;
            case 3:
                maintenanceOrderVo.setResultStatus(3);
                maintenanceOrderVo.setResultStatusMsg("已完成");
                break;
            case 4:
                maintenanceOrderVo.setResultStatus(4);
                maintenanceOrderVo.setResultStatusMsg("已拒绝");
                break;
            default:
                break;
        }

        return maintenanceOrderVo;
    }

    @Transactional
    public void feedback(FeedbackRequest feedbackRequest, Integer customerUserId) {
        try {
            Integer feedback = feedbackRequest.getFeedback();
            if(feedback!=1 && feedback!=2){
                throw new Exception("业主标记出错。。");
            }
            MaintenanceOrderHistory orderHistory = new MaintenanceOrderHistory();
            orderHistory.setOrderId(feedbackRequest.getOrderId());
            orderHistory.setOperateUserId(getMaintenanceUser(customerUserId).getId());
            orderHistory.setOperateAction(feedback ==2?OPRATE_UNRESOLVE:OPRATE_RESOLVE);
            orderHistory.setOperateDetail(feedbackRequest.getDetail());
            orderHistory.setOperateTime(new Date());
            if(!addOrderHistory(orderHistory)){
                throw new Exception("标记失败");
            }
            maintenanceOrderMapper.feedback(feedbackRequest.getOrderId(), feedback);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public MaintenanceOrderDetailVo orderDetail(Integer orderId, Integer customerId) {
        MaintenanceOrder maintenanceOrder = maintenanceOrderMapper.getDetailById(orderId);
        MaintenanceOrderDetailVo maintenanceOrderDetailVo = new MaintenanceOrderDetailVo();
        BeanUtils.copyProperties(maintenanceOrder, maintenanceOrderDetailVo);
        if(maintenanceOrder.getFaultPhoto()!=null){
            maintenanceOrderDetailVo.setFaultPhoto(Arrays.asList(maintenanceOrder.getFaultPhoto().split(",")));
        }
        if(maintenanceOrder.getResultPhoto()!=null){
            maintenanceOrderDetailVo.setResultPhoto(Arrays.asList(maintenanceOrder.getResultPhoto().split(",")));
        }
        List<MaintenanceUser> userList = maintenanceUserMapper.selectManageUserList(customerId);
        if(userList.size()>0){
            maintenanceOrderDetailVo.setManageUserPhone(userList.get(0).getPhone());
        }
        return maintenanceOrderDetailVo;
    }

    public void evaluation(Integer orderId, Integer star, String evaluation) {
        maintenanceOrderMapper.evaluation(orderId, star, evaluation);
    }

    public void userMsg(Integer customerId, Integer customerUserId,Integer userId, String name, String phone) {
        MaintenanceUser maintenanceUser = new MaintenanceUser();
        maintenanceUser.setId(userId);
        maintenanceUser.setCustomerUserId(customerUserId);
        maintenanceUser.setCustomerId(customerId);
        maintenanceUser.setName(name);
        maintenanceUser.setPhone(phone);
        maintenanceUser.setCreateTime(new Date());
        maintenanceUser.setRole(1);  //角色：1普通人，2维修人员，3管理人员
        maintenanceUser.setStatus(1);
        if(userId == null){
            maintenanceUserMapper.insert(maintenanceUser);
        }else{
            maintenanceUserMapper.updateById(maintenanceUser);
        }
    }


    public void resultPhoto(Integer orderId, List<String> resultPhoto) {
        maintenanceOrderMapper.updateResultPhoto(orderId, String.join(",", resultPhoto));
    }

    public void neglectOrder(Integer orderId) {
        maintenanceOrderMapper.neglectOrder(orderId);
    }

    public List<MaintenanceWbUserVo> obtainWbUsers() {
        List<MaintenanceWbUserVo> maintenanceWbUserVoList = new ArrayList<>();
        List<MaintenanceUser> maintenanceUsers = maintenanceUserMapper.obtainWbUsers();
        for (MaintenanceUser maintenanceUser : maintenanceUsers) {
            MaintenanceWbUserVo maintenanceWbUserVo = new MaintenanceWbUserVo();
            BeanUtils.copyProperties(maintenanceUser, maintenanceWbUserVo);
            maintenanceWbUserVoList.add(maintenanceWbUserVo);
        }
        return maintenanceWbUserVoList;
    }

    @Transactional
    public String distribute(DistributeRequest distributeRequest, Integer customerUserId) throws Exception {

            MaintenanceOrder maintenanceOrder = maintenanceOrderMapper.selectById(distributeRequest.getOrderId());
            Integer wbUserId = distributeRequest.getWbUserId();
            MaintenanceUser maintenanceUser = maintenanceUserMapper.selectById(wbUserId);
            if(maintenanceUser.getWorkStatus() == 0){
                throw new Exception("维保人员已请假");
            }
            //查询维保人员维修组是否已存在该设备
            Integer count = deviceTeamItemMapper.selectWbUserExistDevice(maintenanceOrder.getDeviceId(), wbUserId);
            if(count > 0){
                throw new Exception("维保人员已存在该维修设备");
            }
            MaintenanceOrderHistory orderHistory = new MaintenanceOrderHistory();
            orderHistory.setOperateUserId(getMaintenanceUser(customerUserId).getId());
            orderHistory.setOrderId(distributeRequest.getOrderId());
            orderHistory.setOperateAction(OPRATE_DISTRIBUTE);
            orderHistory.setOperateDetail(distributeRequest.getDetail());
            orderHistory.setOperateTime(new Date());
            if(!addOrderHistory(orderHistory)){
                throw new Exception("派单失败");
            }
            //修改接单数
            maintenanceUser.setOrderNum(maintenanceUser.getOrderNum() + 1);
            maintenanceUserMapper.updateOrderNumAndReceiveStatus(maintenanceUser.getId(), maintenanceUser.getOrderNum());

            //给维保人员绑定设备
            DeviceTeamPo deviceTeamPo = new DeviceTeamPo();
            deviceTeamPo.setName(WB_TEAM_NAME);
            deviceTeamPo.setMasterUserId(maintenanceUser.getCustomerUserId());
            deviceTeamPo.setCreateUserId(maintenanceUser.getCustomerUserId());
            DeviceTeamPo deviceTeamPo1 = deviceTeamMapper.queryByName(deviceTeamPo);
            DeviceTeamItemPo deviceTeamItemPo = new DeviceTeamItemPo();
            deviceTeamItemPo.setDeviceId(maintenanceOrder.getDeviceId());
            deviceTeamItemPo.setTeamId(deviceTeamPo1.getId());
            deviceTeamItemPo.setUserId(deviceTeamPo1.getMasterUserId());
            deviceTeamItemPo.setStatus(1);
            deviceTeamItemPo.setCreateTime(System.currentTimeMillis());
            deviceTeamItemMapper.insert(deviceTeamItemPo);
            maintenanceOrderMapper.distribute(distributeRequest.getOrderId(), distributeRequest.getWbUserId());
            return maintenanceUser.getPhone();
    }

    public List<MaintenanceOrderVo> manegeOrderlist(Integer orderStatus) {
        //0:全部 1：待分配 2：待维修  3：待审核  4：已驳回  5：已完成  6：已忽略
        List<MaintenanceOrderVo> maintenanceOrderVoList = new ArrayList<>();
        switch (orderStatus) {
            case 0:  //全部
                List<MaintenanceOrder> maintenanceOrders = maintenanceOrderMapper.selectManageOrderList(orderStatus);
                for (MaintenanceOrder maintenanceOrder : maintenanceOrders) {
                    MaintenanceOrderVo maintenanceOrderVo = new MaintenanceOrderVo();
                    BeanUtils.copyProperties(maintenanceOrder, maintenanceOrderVo);
                    getManageResultStatus(maintenanceOrder, maintenanceOrderVo);
                    maintenanceOrderVoList.add(maintenanceOrderVo);
                }
                break;
            case 1:
                List<MaintenanceOrder> maintenanceOrders1 = maintenanceOrderMapper.selectManageOrderList1(orderStatus);
                for (MaintenanceOrder maintenanceOrder : maintenanceOrders1) {
                    MaintenanceOrderVo maintenanceOrderVo = new MaintenanceOrderVo();
                    BeanUtils.copyProperties(maintenanceOrder, maintenanceOrderVo);
                    getManageResultStatus(maintenanceOrder, maintenanceOrderVo);
                    maintenanceOrderVoList.add(maintenanceOrderVo);
                }
                break;
            case 2:
                List<MaintenanceOrder> maintenanceOrders2 = maintenanceOrderMapper.selectManageOrderList2(orderStatus);
                for (MaintenanceOrder maintenanceOrder : maintenanceOrders2) {
                    MaintenanceOrderVo maintenanceOrderVo = new MaintenanceOrderVo();
                    BeanUtils.copyProperties(maintenanceOrder, maintenanceOrderVo);
                    getManageResultStatus(maintenanceOrder, maintenanceOrderVo);
                    maintenanceOrderVoList.add(maintenanceOrderVo);
                }
                break;
            case 3:
                List<MaintenanceOrder> maintenanceOrders3 = maintenanceOrderMapper.selectManageOrderList3(orderStatus);
                for (MaintenanceOrder maintenanceOrder : maintenanceOrders3) {
                    MaintenanceOrderVo maintenanceOrderVo = new MaintenanceOrderVo();
                    BeanUtils.copyProperties(maintenanceOrder, maintenanceOrderVo);
                    getManageResultStatus(maintenanceOrder, maintenanceOrderVo);
                    maintenanceOrderVoList.add(maintenanceOrderVo);
                }
                break;
            case 4:
                List<MaintenanceOrder> maintenanceOrders4 = maintenanceOrderMapper.selectManageOrderList4(orderStatus);
                for (MaintenanceOrder maintenanceOrder : maintenanceOrders4) {
                    MaintenanceOrderVo maintenanceOrderVo = new MaintenanceOrderVo();
                    BeanUtils.copyProperties(maintenanceOrder, maintenanceOrderVo);
                    getManageResultStatus(maintenanceOrder, maintenanceOrderVo);
                    maintenanceOrderVoList.add(maintenanceOrderVo);
                }
                break;
            case 5:
                List<MaintenanceOrder> maintenanceOrders5 = maintenanceOrderMapper.selectManageOrderList5(orderStatus);
                for (MaintenanceOrder maintenanceOrder : maintenanceOrders5) {
                    MaintenanceOrderVo maintenanceOrderVo = new MaintenanceOrderVo();
                    BeanUtils.copyProperties(maintenanceOrder, maintenanceOrderVo);
                    getManageResultStatus(maintenanceOrder, maintenanceOrderVo);
                    maintenanceOrderVoList.add(maintenanceOrderVo);
                }
                break;
            case 6:
                List<MaintenanceOrder> maintenanceOrders6 = maintenanceOrderMapper.selectManageOrderList6(orderStatus);
                for (MaintenanceOrder maintenanceOrder : maintenanceOrders6) {
                    MaintenanceOrderVo maintenanceOrderVo = new MaintenanceOrderVo();
                    BeanUtils.copyProperties(maintenanceOrder, maintenanceOrderVo);
                    getManageResultStatus(maintenanceOrder, maintenanceOrderVo);
                    maintenanceOrderVoList.add(maintenanceOrderVo);
                }
                break;
            default:
                break;
        }
        return maintenanceOrderVoList;
    }

    private MaintenanceOrderVo getManageResultStatus(MaintenanceOrder maintenanceOrder, MaintenanceOrderVo maintenanceOrderVo) {
        Integer preStatus = maintenanceOrder.getPreStatus();
        Integer wbStatus = maintenanceOrder.getWbStatus();
        switch (preStatus) {
            case 1:
                maintenanceOrderVo.setResultStatus(1);
                maintenanceOrderVo.setResultStatusMsg("待分配");
                break;
            case 2:
                switch (wbStatus) {
                    case 1:     //1待维修，2待确认（管理员待审核），3已完成，4已拒绝
                        maintenanceOrderVo.setResultStatus(2);
                        maintenanceOrderVo.setResultStatusMsg("待维修");
                        break;
                    case 2:
                        maintenanceOrderVo.setResultStatus(3);
                        maintenanceOrderVo.setResultStatusMsg("待校核");
                        break;
                    case 3:

                        maintenanceOrderVo.setResultStatus(4);
                        maintenanceOrderVo.setResultStatusMsg("已完成");

                        break;
                    case 4:
                        maintenanceOrderVo.setResultStatus(5);
                        maintenanceOrderVo.setResultStatusMsg("已驳回");
                        break;
                    default:
                        break;
                }
                break;
            case 3:
                maintenanceOrderVo.setResultStatus(6);
                maintenanceOrderVo.setResultStatusMsg("已忽略");
                break;
            default:
                break;
        }
        return maintenanceOrderVo;
    }




    private MaintenanceUser getMaintenanceUser(Integer customerUserId) throws Exception {
        MaintenanceUser maintenanceUser = maintenanceUserMapper.selectByCustomerUserId(customerUserId);
        if(maintenanceUser == null){
            throw new Exception("用户不存在");
        }
        return maintenanceUser;
    }

    private Boolean addOrderHistory(MaintenanceOrderHistory orderHistory){
        return maintenanceOrderHistoryMapper.insert(orderHistory) > 0;
    }

    @Transactional
    public void finish(Integer orderId, Integer wbStatus, Integer customerUserId) {
        //value 3已完成，4已拒绝
        try {
            MaintenanceOrder maintenanceOrder = maintenanceOrderMapper.selectById(orderId);
            MaintenanceUser maintenanceUser = maintenanceUserMapper.selectById(maintenanceOrder.getWbUserId());
            if(wbStatus !=3 && wbStatus !=4){
                throw new Exception();
            }
            maintenanceOrderMapper.finish(orderId, wbStatus);
            MaintenanceOrderHistory orderHistory = new MaintenanceOrderHistory();
            orderHistory.setOperateUserId(getMaintenanceUser(customerUserId).getId());
            orderHistory.setOrderId(orderId);
            orderHistory.setOperateAction(wbStatus ==3?OPRATE_FINISH:OPRATE_REFUSE);
            orderHistory.setOperateTime(new Date());
            if(!addOrderHistory(orderHistory)){
                throw new Exception("标记失败");
            }
            //解除维保人员绑定设备
            DeviceTeamItemPo deviceTeamItemPo = deviceTeamItemMapper.selectByJoinId(maintenanceOrder.getDeviceId(), maintenanceUser.getCustomerUserId());
            deviceTeamItemMapper.deleteById(deviceTeamItemPo.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public MaintenanceUserMsgVo getUserMsg(Integer customerUserId) {
        MaintenanceUser userMsg = maintenanceUserMapper.getUserMsg(customerUserId);
        MaintenanceUserMsgVo maintenanceUserMsgVo = new MaintenanceUserMsgVo();
        if(userMsg==null){
            return null;
        }
        BeanUtils.copyProperties(userMsg, maintenanceUserMsgVo);
        return maintenanceUserMsgVo;
    }

    public MaintenanceWbUserVo login(MaintenanceManageLoginRequest request, Integer customerUserId, Integer customerId) {
        String name = request.getName();
        String password = MD5Util.md5(MD5Util.md5(request.getPasswd()) + saltEncrypt);
        MaintenanceWbUserVo userVo = maintenanceUserMapper.login(name, password, customerId);
        return userVo;
    }

    public MaintenanceUserMsgVo getWbUserMsg(Integer customerUserId) {
        MaintenanceUser userMsg = maintenanceUserMapper.getWbUserMsg(customerUserId);
        MaintenanceUserMsgVo maintenanceUserMsgVo = new MaintenanceUserMsgVo();
        if(userMsg==null){
            return null;
        }
        BeanUtils.copyProperties(userMsg, maintenanceUserMsgVo);
        return maintenanceUserMsgVo;
    }
}
